#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author   : long.hai
@Contact  : 1982964634@qq.com
"""
import traceback

from playwright.async_api import Playwright, async_playwright
from _base.errors import ConnectError
from _base.browser import BrowserAction
from _base.session import SessionState
from _base.page import PageAction


class BasePage(BrowserAction):

    def __init__(self, playwright: Playwright, ws=None):
        self.playwright = playwright
        self.ws = ws
        BrowserAction.__init__(self, playwright.chromium, self.ws)

    async def conn_debug_chrome(self):
        """连接调试浏览器"""
        try:
            await self.log_info('即将连接浏览器。')
            self.browser = await self.playwright.chromium.connect_over_cdp("http://localhost:9223")
            await self.get_debug_page()
            await self.log_info('连接浏览器并获取页面成功，设置浏览器超时时间为5000ms。')
        except:
            traceback.print_exc()
            raise ConnectError('连接浏览器失败。\n1.请检查调试模式浏览器是否打开。\n2.调试模式是否使用9222端口。')


# import asyncio
#
#
# async def run_init():
#     async with async_playwright() as playwright:
#         bp = BasePage(playwright, None)
#         try:
#             await bp.conn_debug_chrome()
#             await bp.goto(**{"params": "https://test.snkoudai.com/index.html"})
#             await bp.click(**{"locators": [{"by": "xpath", "params": '//p[@title="WEB自动化测试"]'}]})
#             await bp.click(**{"locators": [{"by": "xpath", "params": '//p[text()="基本信息"]'}]})
#             await bp.fill(**{"locators": [{"by": "get_by_placeholder", "params": '例如：鹅湖香米生产基地'}], "params": "123123"})
#             # await browser.close()
#         except Exception as e:
#             await bp.log_error(str(e))
#         finally:
#             # print(bp.log_text)
#             pass
#         # print(bp.log_text)
#
#
# #         补贴申请通知
#
# asyncio.run(run_init())
